package com.lipp.luosu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lipp.luosu.dto.req.BillSearchReqDto;
import com.lipp.luosu.dto.resp.PageRespDto;
import com.lipp.luosu.mapper.BillMapper;
import com.lipp.luosu.pojo.Bill;
import com.lipp.luosu.service.BillService;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantLock;

@Service
@Slf4j
public class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements BillService {

    /**
     * 获取列表
     *
     * @param billSearchReqDto 搜索条件请求对象
     * @return 页面响应对象
     */
    @Override
    public PageRespDto<Bill> getList(BillSearchReqDto billSearchReqDto) {
        LambdaQueryWrapper<Bill> queryWrapper = new LambdaQueryWrapper(); // 创建查询条件对象   绷不住，怪不得售前没办法走流程给我分配项目，真他娘有党争，反正我不管啊，谁给我派活就做，不给我
        if (!ObjectUtils.isEmpty(billSearchReqDto.getItem())) {
            queryWrapper.like(Bill::getItem, billSearchReqDto.getItem().trim()); // 根据搜索条件进行模糊查询
        }
        if (!ObjectUtils.isEmpty(billSearchReqDto.getStartDate())) {
            queryWrapper.ge(Bill::getCreateDate, billSearchReqDto.getStartDate()); // 根据搜索条件进行日期查询（小于等于）
        }
        if (!ObjectUtils.isEmpty(billSearchReqDto.getEndDate())) {
            queryWrapper.le(Bill::getCreateDate, billSearchReqDto.getEndDate()); // 根据搜索条件进行日期查询（大于等于）
        }
        if (!ObjectUtils.isEmpty(billSearchReqDto.getType())) {
            queryWrapper.eq(Bill::getType, billSearchReqDto.getType().trim()); // 根据搜索条件进行等值查询
        }
        if (!ObjectUtils.isEmpty(billSearchReqDto.getMoney())) {
            queryWrapper.eq(Bill::getMoney, billSearchReqDto.getMoney()); // 根据搜索条件进行等值查询
        }
        if (!ObjectUtils.isEmpty(billSearchReqDto.getOperateStatus())) {
            queryWrapper.eq(Bill::getOperateStatus, billSearchReqDto.getOperateStatus()); // 根据搜索条件进行等值查询
        }
        // page == limit pageNum , pageSize
        log.info("查询的第" + billSearchReqDto.getPageNum() + "页，每页" + billSearchReqDto.getPageSize() + "条");
        Page<Bill> page = new Page<>(billSearchReqDto.getPageNum(), billSearchReqDto.getPageSize()); // 创建分页对象
        IPage<Bill> billIPage = this.page(page, queryWrapper); // 根据分页对象和查询条件进行分页查询
        PageRespDto<Bill> pageRespDto = new PageRespDto<>(); // 创建页面响应对象
        pageRespDto.setTotal(billIPage.getTotal()); // 设置总记录数
        pageRespDto.setEntityLists(billIPage.getRecords()); // 设置查询结果
        return pageRespDto; // 返回页面响应对象
    }

    @Override
    public void deleteBill(String id) {
        this.remove(new QueryWrapper<Bill>().eq("id", id));
    }

    @Override
    public void saveBill(Bill bill) {
        try {
            if (ObjectUtils.isEmpty(bill.getId())) {
                bill.setId(UUID.randomUUID().toString());
                log.info("新增账单");
                this.save(bill);
            } else {
                log.info("修改账单");
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String formattedDate = sdf.format(date);
                bill.setUpdateDate(formattedDate);
                this.updateById(bill);
            }
        } catch (Exception e) {
            throw new RuntimeException("保存账单失败" + e.getMessage());
        }

    }

    private static ReentrantLock lock = new ReentrantLock();
    private volatile int count = 0;

    @Override
    public void test() {
        try {
            lock.lock();
            log.info("测试");
            LambdaQueryWrapper<Bill> queryWrapper = new LambdaQueryWrapper();

            queryWrapper.eq(Bill::getOperateStatus, "1");


            Bill one = this.getOne(queryWrapper);
            if (ObjectUtils.isEmpty(one)) {
                log.info("查询结果为空");
                Bill bill = new Bill();
                bill.setId(UUID.randomUUID().toString());
                bill.setItem("测试");
                bill.setOperateStatus("1");
                bill.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date()));
                bill.setMoney("" + count++);
                log.info(bill.toString());
                this.save(bill);
            } else {
                log.info("查询结果为：" + one.toString());
            }

        } catch (Exception e) {
            log.error("查询失败" + e.getMessage());
        } finally {
            lock.unlock();
        }


    }

    @Override
    public void a() {
        Bill bill = new Bill();
        bill.setId(UUID.randomUUID().toString());
        bill.setItem("测试");
        bill.setOperateStatus("1");
        bill.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
        bill.setMoney("" + count++);
        log.info(bill.toString());
        this.save(bill);

    }

}